﻿using Dynamics.NAV.Metro.Common;
using Dynamics.NAV.Metro.Data;
using Okra.Core;
using Okra.Navigation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Contacts.Provider;

namespace Dynamics.NAV.Metro.Pages.GLAccountDetail
{
    public class GLAccountDetailViewModel : BindableBase, IActivatable<NavGLAccount, string>
    {
        private IList<NavGLEntry> _glEntries;
        
        public GLAccountDetailViewModel()
        {
            this.GoBackCommand = new DelegateCommand(GoBack);
        }

        [Import]
        public INavGLEntryDataSource NavDataSource { get; set; }

        [Import]
        public INavigationManager NavigationManager { get; set; }
 
        public ICommand GoBackCommand { get; private set; }

        public bool CanGoBack
        {
            get { return NavigationManager.CanGoBack; }
        }

        public void GoBack()
        {
            NavigationManager.GoBack();
        }

        public void Activate(NavGLAccount arguments, string state)
        {
            this.SelectedGLAccount = arguments;
            if (SelectedGLAccount != null)
            {
                if (NavDataSource == null)
                    NavDataSource = new NavGLEntryDataSource();

                this.GLEntries = NavDataSource.GetGLEntries(SelectedGLAccount);
            }
        }

        public string SaveState()
        {
            return null;
        }

        public NavGLAccount SelectedGLAccount { get; set; }

        public IList<NavGLEntry> GLEntries
        {
            get
            {
                return _glEntries;
            }
            set
            {
                if (_glEntries != value)
                {
                    _glEntries = value;
                    OnPropertyChanged("GLEntries");
                }
            }
        }
    }
}
